package tc.crase.cmds;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.util.Iterator;
import javax.swing.JLabel;
import javax.swing.JPanel;

import tc.common.symbology.Symbol;
import tc.common.symbology.SymbolItemIregular;
import tc.common.utility.Dynbuff;
import tc.common.utility.DynbuffLine;
import tc.common.utility.DynbuffRect;
import tc.common.utility.DynbuffSymbol;
import tc.common.utility.GrObject;
import tc.common.utility.GrView;
import tc.common.utility.TcCmd;
import tc.common.utility.TcDisplayXY;
import tc.common.utility.Vector2D;

public class CmdViewMove extends TcCmd {
	private boolean pointEntered;
	private Point point;
	
	JLabel output;
	
	public CmdViewMove () {
		pointEntered = false;
		/*prev  = false;
		prev1 = new Point();
		prev2 = new Point();*/
		
		output = new JLabel("Nothing entered");
	}
	
	public void initializeDialog() {
		super.initializeDialog();
		setLabel("Zoom");
		setHint("Enter first point.");
	}
	
	public void enterPoint(Point pt, GrView view) {
		if (pointEntered ) {
			
			TcDisplayXY xy0 = new TcDisplayXY(new Point2D.Double(point.x, point.y), view.transf, TcDisplayXY.CoordSys.DEVICE);
			xy0.transform(TcDisplayXY.CoordSys.WORLD);
			
			TcDisplayXY xy1 = new TcDisplayXY(new Point2D.Double(pt.x, pt.y), view.transf, TcDisplayXY.CoordSys.DEVICE);
			xy1.transform(TcDisplayXY.CoordSys.WORLD);
			
			Vector2D delta = new Vector2D(xy1.xy, xy0.xy);
			delta.addTo( view.transf.center );
			
			view.repaint();
			
			pointEntered = false;
			hint.setText("Enter first point.");
		}
		else {
			point = pt; 
		    pointEntered = true;
		    hint.setText("Enter second point.");
		}
		
		Dynbuff.dynamicBufferClean();
	}
	public void enterReset(GrView view) {
	}
	public void enterMove(Point pt, GrView view)  {
		Dynbuff.dynamicBufferClean();
		if ( pointEntered == false ) return;
		
		DynbuffLine line = new DynbuffLine();
		line.points.add(new Point(point.x, point.y));
		line.points.add(new Point(pt.x, pt.y));
		Dynbuff.dynamicBufferAdd(line, Dynbuff.Buffer.CMD, (JPanel)view);
		
		int dx = pt.x-point.x;
		int dy = pt.y-point.y;
		int w = view.getWidth();
		int h = view.getHeight();
		Point2D.Double c = view.transf.center;
		TcDisplayXY xyC = new TcDisplayXY(new Point2D.Double(c.x, c.y), view.transf, TcDisplayXY.CoordSys.DEVICE);
		xyC.transform(TcDisplayXY.CoordSys.WORLD);
		int cx = (int)xyC.xy.x;
		int cy = (int)xyC.xy.y;
		
	    Symbol arrow = new Symbol("");
	    
	    SymbolItemIregular item = new SymbolItemIregular(SymbolItemIregular.Type.LINE, Color.BLACK);
	    item.points.add(Symbol.computePoint(-1.0, -0.6));
	    item.points.add(Symbol.computePoint( 1.0,  0.0));
	    item.points.add(Symbol.computePoint(-1.0,  0.6));
	    arrow.items.add(item);
		
		DynbuffSymbol symbol = new DynbuffSymbol(pt, arrow, dx, dy);
		Dynbuff.dynamicBufferAdd(symbol, Dynbuff.Buffer.CMD, view);
	}
}